﻿using System;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;

namespace Cubicle.Core.DataContracts
{
    [DataContract]
    [Serializable]
    public class PropertyData
    {

        [DataMember]
        public string PropertyName { get; set; }

        [DataMember]
        public string PropertyType { get; set; }

        [DataMember]
        public string DeclaringType { get; set; }

        [DataMember]
        public bool GetIsPublic { get; set; }

        [DataMember]
        public bool GetIsPrivate { get; set; }

        [DataMember]
        public bool GetIsAssembly { get; set; }

        [DataMember]
        public bool GetIsFamily { get; set; }

        [DataMember]
        public bool GetIsFamilyAndAssembly { get; set; }

        [DataMember]
        public bool GetIsFamilyOrAssembly { get; set; }

        [DataMember]
        public bool SetIsPublic { get; set; }

        [DataMember]
        public bool SetIsPrivate { get; set; }

        [DataMember]
        public bool SetIsAssembly { get; set; }

        [DataMember]
        public bool SetIsFamily { get; set; }

        [DataMember]
        public bool SetIsFamilyAndAssembly { get; set; }

        [DataMember]
        public bool SetIsFamilyOrAssembly { get; set; }

        public PropertyData(PropertyInfo x)
        {
            PropertyName = x.Name;
            PropertyType = x.PropertyType.FullName;
            DeclaringType = x.DeclaringType.FullName;
            var getAccessor = x.GetAccessors(true).Where(y => y.Name.StartsWith("get")).FirstOrDefault();
            GetIsPublic = getAccessor == null ? false : getAccessor.IsPublic;
            GetIsPrivate = getAccessor == null ? false : getAccessor.IsPrivate;
            GetIsAssembly = getAccessor == null ? false : getAccessor.IsAssembly;
            GetIsFamily = getAccessor == null ? false : getAccessor.IsFamily;
            GetIsFamilyAndAssembly = getAccessor == null ? false : getAccessor.IsFamilyAndAssembly;
            GetIsFamilyOrAssembly = getAccessor == null ? false : getAccessor.IsFamilyOrAssembly;
            var setAccessor = x.GetAccessors(true).Where(y => y.Name.StartsWith("set")).FirstOrDefault();
            SetIsPublic = setAccessor == null ? false : setAccessor.IsPublic;
            SetIsPrivate = setAccessor == null ? false : setAccessor.IsPrivate;
            SetIsAssembly = setAccessor == null ? false : setAccessor.IsAssembly;
            SetIsFamily = setAccessor == null ? false : setAccessor.IsFamily;
            SetIsFamilyAndAssembly = setAccessor == null ? false : setAccessor.IsFamilyAndAssembly;
            SetIsFamilyOrAssembly = setAccessor == null ? false : setAccessor.IsFamilyOrAssembly;
        }

    }
}